package com.HospitalOutpatient.service.impl;

import com.HospitalOutpatient.dao.DoctorDao;
import com.HospitalOutpatient.dao.DutyDao;
import com.HospitalOutpatient.dao.OutpatientDao;
import com.HospitalOutpatient.dto.DoctorDutyDto;
import com.HospitalOutpatient.dto.DutyDto;
import com.HospitalOutpatient.entity.Doctor;
import com.HospitalOutpatient.entity.Duty;
import com.HospitalOutpatient.entity.Outpatient;
import com.HospitalOutpatient.service.DutyService;
import com.HospitalOutpatient.utils.DateUtil;
import com.HospitalOutpatient.vo.DoctorDutyVo;
import com.HospitalOutpatient.vo.DutyIdAndPartialNum;
import com.HospitalOutpatient.vo.DutyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("dutyService")
public class DutyServiceImpl implements DutyService {

    @Autowired
    private DutyDao dutyDao;

    @Autowired
    private DoctorDao doctorDao;

    @Autowired
    private OutpatientDao outpatientDao;

    @Override
    public boolean addDuty(List<DutyDto> dutyList) {
        List<Duty> duties = new ArrayList<>();

        String doctorId = dutyList.get(0).getDoctorId();
        String outpatientName = dutyList.get(0).getOutpatientName();

        String dutyDate = DateUtil.dateToString(dutyList.get(0).getDutyDate());


        Outpatient outpatient = outpatientDao.getOutpatientByName(outpatientName);



        for (DutyDto dutyDto : dutyList) {
            if(dutyDto.getTotalNum() == null || dutyDto.getTotalNum() == ""){
                continue;
            }
            try {
                Date startTime = DateUtil.mergeDate(dutyDto.getDutyDate(), dutyDto.getStartTime());
                Date endTime = DateUtil.mergeDate(dutyDto.getDutyDate(), dutyDto.getEndTime());

                Duty duty = new Duty();

                duty.setDoctorId(doctorId);
                duty.setOutpatientId(outpatient.getOutpatientId());
                duty.setStartTime(DateUtil.dateToString(startTime));
                duty.setEndTime(DateUtil.dateToString(endTime));
                duty.setDutyDate(DateUtil.dateToString(dutyDto.getDutyDate()));
                duty.setTotalNum(dutyDto.getTotalNum());
                duty.setPartialNum(dutyDto.getTotalNum());

                duties.add(duty);

            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        Duty duty = new Duty();
        duty.setDutyDate(dutyDate);
        duty.setDoctorId(doctorId);

        dutyDao.deleteDutyByDate(duty);

        return dutyDao.insertDuty(duties) >= 1;
    }

    @Override
    public List<DutyVo> getDutyByIdandDate(DutyDto dutyDto) {
        List<DutyVo> dutyVoList = new ArrayList<>();
        Duty duty = new Duty();

        duty.setDoctorId(dutyDto.getDoctorId());
        duty.setDutyDate(DateUtil.dateToString(dutyDto.getDutyDate()));

        List<Duty> dutyList = dutyDao.selectAllDutyByIdAndDate(duty);
        for (Duty duty1 : dutyList) {
            DutyVo dutyVo = new DutyVo();

            String dateTime = duty1.getStartTime()+" ~ "+duty1.getEndTime();

            dutyVo.setTotalNum(duty1.getTotalNum());
            dutyVo.setDateTime(dateTime);

            dutyVoList.add(dutyVo);
        }
        return dutyVoList;
    }

    @Override
    public List<DoctorDutyVo> getDoctorDutyByDateAndOutpatientId(DoctorDutyDto doctorDutyDto) {

        List<Duty> dutyList = dutyDao.selectDutyByOutpatientIdAndDatethanTime(doctorDutyDto);

        List<DoctorDutyVo> doctorDutyVoList = new ArrayList<>();
        for (Duty duty1 : dutyList) {
            int sumPartialNum = dutyDao.selectSumpartialNumByDoctorId(duty1);
            if(sumPartialNum <= 0){
                continue;
            }

            Doctor doctor = doctorDao.selectDoctorById(duty1.getDoctorId());

            int sumTotalNum = dutyDao.selectSumtotalNumByDoctorId(duty1);

            DoctorDutyVo doctorDutyVo = new DoctorDutyVo();

            doctorDutyVo.setDoctorId(doctor.getDoctorId());
            doctorDutyVo.setDoctorName(doctor.getDoctorName());
            doctorDutyVo.setPosition(doctor.getPosition());
            doctorDutyVo.setIntroduction(doctor.getIntroduction());
            doctorDutyVo.setCharge(doctor.getCharge());
            doctorDutyVo.setPartialNum(String.valueOf(sumPartialNum));
            doctorDutyVo.setTotalNum(String.valueOf(sumTotalNum));

            doctorDutyVoList.add(doctorDutyVo);
        }

        return doctorDutyVoList;
    }

    @Override
    public List<DutyIdAndPartialNum> getDutyByDoctorIdAndTime(Duty duty) {

        List<Duty> dutyList = dutyDao.selectDutyByDoctorIdAndDateTime(duty);

        List<DutyIdAndPartialNum> dutyIdAndPartialNumList = new ArrayList<>();

        for (Duty duty1 : dutyList) {

            String dateTime = duty1.getStartTime()+" ~ "+duty1.getEndTime();

            DutyIdAndPartialNum dutyIdAndPartialNum = new DutyIdAndPartialNum();

            dutyIdAndPartialNum.setDutyId(duty1.getDutyId());
            dutyIdAndPartialNum.setPartialNum(duty1.getPartialNum());
            dutyIdAndPartialNum.setDateTime(dateTime);

            dutyIdAndPartialNumList.add(dutyIdAndPartialNum);
        }
        return dutyIdAndPartialNumList;
    }
}
